/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.data.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.data.entity.FishData;
import vip.xiaonuo.biz.modular.data.mapper.FishDataMapper;
import vip.xiaonuo.biz.modular.data.param.FishDataAddParam;
import vip.xiaonuo.biz.modular.data.param.FishDataEditParam;
import vip.xiaonuo.biz.modular.data.param.FishDataIdParam;
import vip.xiaonuo.biz.modular.data.param.FishDataPageParam;
import vip.xiaonuo.biz.modular.data.service.FishDataService;

import java.util.List;

/**
 * 鱼种类Service接口实现类
 *
 * @author jie
 * @date  2024/03/01 16:51
 **/
@Service
public class FishDataServiceImpl extends ServiceImpl<FishDataMapper, FishData> implements FishDataService {

    @Override
    public Page<FishData> page(FishDataPageParam fishDataPageParam) {
        QueryWrapper<FishData> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getNamedByMan())) {
            queryWrapper.lambda().eq(FishData::getNamedByMan, fishDataPageParam.getNamedByMan());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getChineseName())) {
            queryWrapper.lambda().eq(FishData::getChineseName, fishDataPageParam.getChineseName());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getBranchChineseName())) {
            queryWrapper.lambda().eq(FishData::getBranchChineseName, fishDataPageParam.getBranchChineseName());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getSectionName())) {
            queryWrapper.lambda().eq(FishData::getSectionName, fishDataPageParam.getSectionName());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getMaxBodyLength())) {
            queryWrapper.lambda().eq(FishData::getMaxBodyLength, fishDataPageParam.getMaxBodyLength());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getWorldDistribution())) {
            queryWrapper.lambda().eq(FishData::getWorldDistribution, fishDataPageParam.getWorldDistribution());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getHabitat())) {
            queryWrapper.lambda().like(FishData::getHabitat, fishDataPageParam.getHabitat());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getSynonyms())) {
            queryWrapper.lambda().like(FishData::getSynonyms, fishDataPageParam.getSynonyms());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getCankaowenxian())) {
            queryWrapper.lambda().like(FishData::getCankaowenxian, fishDataPageParam.getCankaowenxian());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getCollectedSpecimens())) {
            queryWrapper.lambda().like(FishData::getCollectedSpecimens, fishDataPageParam.getCollectedSpecimens());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getEnglishCommonName())) {
            queryWrapper.lambda().like(FishData::getEnglishCommonName, fishDataPageParam.getEnglishCommonName());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getMorphologicalCharacteristics())) {
            queryWrapper.lambda().like(FishData::getMorphologicalCharacteristics, fishDataPageParam.getMorphologicalCharacteristics());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getHabitatEcology())) {
            queryWrapper.lambda().like(FishData::getHabitatEcology, fishDataPageParam.getHabitatEcology());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getGeographicalDistribution())) {
            queryWrapper.lambda().like(FishData::getGeographicalDistribution, fishDataPageParam.getGeographicalDistribution());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getFisheryUtilization())) {
            queryWrapper.lambda().like(FishData::getFisheryUtilization, fishDataPageParam.getFisheryUtilization());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getHabitatDepth())) {
            queryWrapper.lambda().like(FishData::getHabitatDepth, fishDataPageParam.getHabitatDepth());
        }
        if(ObjectUtil.isNotEmpty(fishDataPageParam.getOtherChineseName())) {
            queryWrapper.lambda().like(FishData::getOtherChineseName, fishDataPageParam.getOtherChineseName());
        }
        if(ObjectUtil.isAllNotEmpty(fishDataPageParam.getSortField(), fishDataPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(fishDataPageParam.getSortOrder());
            queryWrapper.orderBy(true, fishDataPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(fishDataPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(FishData::getScientificName);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(FishDataAddParam fishDataAddParam) {
        FishData fishData = BeanUtil.toBean(fishDataAddParam, FishData.class);
        this.save(fishData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(FishDataEditParam fishDataEditParam) {
        FishData fishData = this.queryEntity(fishDataEditParam.getScientificName());
        BeanUtil.copyProperties(fishDataEditParam, fishData);
        this.updateById(fishData);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<FishDataIdParam> fishDataIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(fishDataIdParamList, FishDataIdParam::getScientificName));
    }

    @Override
    public FishData detail(FishDataIdParam fishDataIdParam) {
        return this.queryEntity(fishDataIdParam.getScientificName());
    }

    @Override
    public FishData queryEntity(String id) {
        FishData fishData = this.getById(id);
        if(ObjectUtil.isEmpty(fishData)) {
            throw new CommonException("鱼种类不存在，id值为：{}", id);
        }
        return fishData;
    }
}
